﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using Cms.Entity.Menu;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Common;
using Cms.BLL.Menu;

namespace Cms.Admin.Menu
{
    public partial class List : PageList
    {
        
        private MenuBO _menu = new MenuBO();
        private string strOrders;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindView();
            }
            AddAttr();
            strSelectIDs = hidSelectIDs.Value;
            hidSelectIDs.Value = "";
            strOrders = hidOrders.Value;
            hidOrders.Value = "";

            JSUtil.SetTableHeight("list", 80);
        }

        private void AddAttr()
        {
            JSUtil.DelConfirm(btnDelete, "栏目");
            JSUtil.MoveTree(btnMove, "CmsMenu", hidSelectIDs, btnRefresh);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkMove, btnMove);
            JSUtil.ClickBtn(lnkSaveOrders, btnSaveOrders);

            //imgRole.Attributes.Add("onclick", "showDlg('" + strDlgUrl + "')");

            //lnkTransModule.Attributes.Add("onclick","showDlg
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);

            int rootID = _menu.GetRootID();//通用的栏目                     

            List<MenuInfo> list = _menu.GetTreeList(rootID);

            lv.DataSource = list;
            lv.DataKeyNames = new string[] { "ID", "UpperID" };
            lv.DataBind();


            if (IsPostBack)
            {
                LVUtil.RestoreTreeListState(lv, hidHideTrIDs.Value);
            }

        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 460, 251);
                HtmlTableRow row = null;
                foreach (Control ctl in e.Item.Controls)
                {
                    if (ctl is HtmlTableRow)
                    {
                        row = (HtmlTableRow)ctl;
                        break;
                    }
                }
                string strID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["ID"].ToString();
                row.Attributes["id"] = strID;
                string upperID = lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["UpperID"].ToString();
                row.Attributes.Add("UpperID", upperID);

                CheckBox chkSelect = (CheckBox)e.Item.FindControl("chkSelect");
                if (upperID == Constants.TREE_ROOT_UPPER_ID.ToString())
                {
                    chkSelect.Enabled = false;
                }

                string strUrl = GlobalVar.WebCurrPath + "Edit.aspx?UpperID=" + this.Encrypt(strID) ;
                HyperLink lnkAdd = (HyperLink)e.Item.FindControl("hypAdd");
                JSUtil.ShowForm(lnkAdd, "增加栏目", null, strUrl, 460, 251);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs == "")
            {
                return;
            }

            //删除栏目将不会删除文章，如果删除了栏目，该栏目下的文章只有通过根栏目查看

            IEnumerable<int> ids = StrUtil.SplitToInt(strSelectIDs);

            _menu.Delete(ids);
            BindView();
        }

        protected void btnSaveOrders_Click(object sender, EventArgs e)
        {
            List<int> ids = new List<int>();
            List<int> orders = new List<int>();
            StrUtil.SplitIDOrders(ids, orders, strOrders);
            _menu.SaveOrders(ids.ToArray(), orders.ToArray());
            BindView();
        }
    }
}
